<?php
/**
 * Created by PhpStorm.
 * User: sogubaby
 * Date: 2019-02-22
 * Time: 14:15
 */

namespace Service;

use App\Jobs\RepayCommonJobQueue;
use App\Jobs\RepayJobQueue;
use App\Jobs\RepayMonthJobQueue;
use App\Models\RepayRecord;
use App\Models\RepayRecordCommon;
use App\Models\RepayRecordMoney;
use App\Models\RepayRecordMonth;
use App\Models\Traits\RepaySummary;
use App\Models\UserBankCard;
use Carbon\Carbon;

trait Repay {
    /**
     * Description:当期还
     * Author:lijiafei
     * @return
     */
    protected function repayCurrent($bank_cards_id){
        //判断当前银行卡是否有效
        $hold_agreement_num = UserBankCard::getHoldAgreementNumById($bank_cards_id);
        if(!$hold_agreement_num){
            $returnData['flag'] = false;
            $returnData['msg'] = '请选择有效的银行卡';
            return $returnData;
        }
        //保存当前银行卡以及以后的所有银行卡信息
        $isSave = RepaySummary::saveBankInfo($bank_cards_id);
        if($isSave === false){
            $returnData['flag'] = false;
            $returnData['msg'] = '还款失败,请稍后重试';
            return $returnData;
        }
        //获取当前应该还哪期的数据
        $repayInfo = RepaySummary::getRepaySummaryLog();
        $result = $this->validateCurrentRepay($repayInfo);
        if(!$result['flag']){
            return $result;
        }

        $month_status = $repayInfo->recordOne->month->status;
        $common_status = $repayInfo->recordOne->common->status;
        $money_status = $repayInfo->recordOne->money->status;
        if($month_status == RepayRecordMonth::STATUS_TWO || $common_status == RepayRecordCommon::STATUS_TWO || $money_status == RepayRecordMoney::STATUS_TWO){
            $returnData['flag'] = false;
            $returnData['msg'] = '正在还款中';
            return $returnData;
        }

        //判断是否达到当日最大次数
        $is_send_api = RepaySummary::checkApiNumber($repayInfo->recordOne->summary_id,$repayInfo->recordOne->uid,RepaySummary::COMMON_API);
        if(!$is_send_api){
            $returnData['flag'] = false;
            $returnData['msg'] = '当日请求次数已达上限';
            return $returnData;
        }

        //判断是否达到当日最大次数
        $is_send_api = RepaySummary::checkApiNumber($repayInfo->recordOne->summary_id,$repayInfo->recordOne->uid,RepaySummary::MONEY_API);
        if(!$is_send_api){
            $returnData['flag'] = false;
            $returnData['msg'] = '当日请求次数已达上限';
            return $returnData;
        }

        if($money_status == RepayRecordMoney::STATUS_ONE || $money_status == RepayRecordMoney::STATUS_THREE)
        {
            //资方队列
            $res = RepayJobQueue::dispatch($repayInfo->recordOne->id);
            RepaySummary::setRepayStatus($repayInfo->recordOne->id,$repayInfo->uid,$repayInfo->recordOne->money->summary_id,$repayInfo->recordOne->money->period,'money',RepayRecord::REPAY_TYPE_MANUAL);
        }elseif ($money_status == RepayRecordMoney::STATUS_FOUR){

            if($month_status == RepayRecordMonth::STATUS_ONE || $month_status == RepayRecordMonth::STATUS_THREE){
                //月保费队列
                $res = RepayMonthJobQueue::dispatch($repayInfo->recordOne->id);
                RepaySummary::setRepayStatus($repayInfo->recordOne->id,$repayInfo->uid,$repayInfo->recordOne->month->summary_id,$repayInfo->recordOne->month->period,'month',RepayRecord::REPAY_TYPE_MANUAL);
            }elseif($month_status == RepayRecordMonth::STATUS_FOUR){
                if($common_status == RepayRecordCommon::STATUS_THREE || $common_status == RepayRecordCommon::STATUS_ONE)
                {
                    //公共平台队列
                    $res = RepayCommonJobQueue::dispatch($repayInfo->recordOne->id);
                    RepaySummary::setRepayStatus($repayInfo->recordOne->id,$repayInfo->uid,$repayInfo->recordOne->common->summary_id,$repayInfo->recordOne->common->period,'common',RepayRecord::REPAY_TYPE_MANUAL);
                }elseif($common_status == RepayRecordCommon::STATUS_FOUR){
                    $returnData['flag'] = true;
                    $returnData['msg'] = '当前期已经还款成功';
                    return $returnData;
                }
            }
        }
        $returnData['flag'] = true;
        $returnData['msg'] = '正在还款中,请等待';
        return $returnData;
    }

    /**
     * Description:验证还当期
     * Author:lijiafei
     * @param $repayInfo
     * @return mixed
     */
    protected function validateCurrentRepay($repayInfo){
        if(!$repayInfo){
            $returnData['flag'] = false;
            $returnData['msg'] = '当前没有还款计划';
            return $returnData;
        }

        if($repayInfo->recordOne->status == RepayRecord::STATUS_TWO){
            //代表这条记录还在轮询中,不要还款
            $returnData['flag'] = false;
            $returnData['msg'] = '正在还款中,不能发起新的还款';
            return $returnData;
        }

        $isCurrentRepay = RepaySummary::isCurrentRepay($repayInfo->id);
        if(!$isCurrentRepay){
            $returnData['flag'] = false;
            $returnData['msg'] = '当前不能进行逐期还款';
            return $returnData;
        }
        $returnData['flag'] = true;
        $returnData['msg'] = '';
        return $returnData;
    }

    /**
     * 全部还清
     * @param $bank_cards_id
     * @return mixed
     * @author luwei
     * @date 2019-03-01 18:32
     */
    protected function repayAll($bank_cards_id){
        //判断当前银行卡是否有效
        $hold_agreement_num = UserBankCard::getHoldAgreementNumById($bank_cards_id);
        if(!$hold_agreement_num){
            $returnData['flag'] = false;
            $returnData['msg'] = '请选择有效的银行卡';
            return $returnData;
        }
        //获取当前应该还哪期
        $repayInfo = RepaySummary::getRepaySummaryLog();
        $result = $this->validateAllRepay($repayInfo);
        if(!$result['flag']){
            return $result;
        }

        //获取全部还清的时候的记录 period = 0的记录
        $info = RepaySummary::getAllRepayLog($repayInfo->id);
        if(!$info){
            $returnData['flag'] = false;
            $returnData['msg'] = '提前结清失败';
            return $returnData;
        }

        $isSave = RepaySummary::saveBankInfo($bank_cards_id);
        if($isSave === false){
            $returnData['flag'] = false;
            $returnData['msg'] = '还款失败,请稍后重试';
            return $returnData;
        }
        //判断是否达到当日最大次数
        $is_send_api = RepaySummary::checkApiNumber($info->recordOne->summary_id,$info->recordOne->uid,RepaySummary::COMMON_API);
        if(!$is_send_api){
            $returnData['flag'] = false;
            $returnData['msg'] = '当日请求次数已达上限';
            return $returnData;
        }

        //判断是否达到当日最大次数
        $is_send_api = RepaySummary::checkApiNumber($info->recordOne->summary_id,$info->recordOne->uid,RepaySummary::MONEY_API);
        if(!$is_send_api){
            $returnData['flag'] = false;
            $returnData['msg'] = '当日请求次数已达上限';
            return $returnData;
        }

        $common_status = $info->recordOne->common->status;
        $money_status = $info->recordOne->money->status;
        if($money_status == RepayRecordMoney::STATUS_ONE || $money_status == RepayRecordMoney::STATUS_THREE){
            //待处理  失败  统一交给队列处理
            $res = RepayJobQueue::dispatch($info->recordOne->id);
            RepaySummary::setRepayStatus($info->recordOne->id,$info->uid);
        }elseif($money_status == RepayRecordMoney::STATUS_FOUR){
            if($common_status == RepayRecordCommon::STATUS_ONE || $common_status == RepayRecordCommon::STATUS_THREE){
                //请求公共方法
                $res = RepayCommonJobQueue::dispatch($info->recordOne->id);
                RepaySummary::setRepayStatus($info->recordOne->id,$info->uid);
            }elseif($common_status == RepayRecordCommon::STATUS_FOUR){
                $returnData['flag'] = true;
                $returnData['msg'] = '当前期已经还款成功';
                return $returnData;
            }
        }
        $returnData['flag'] = true;
        $returnData['msg'] = '正在还款中,请等待';
        return $returnData;
    }
    /**
     * 验证全部还清
     */
    protected function validateAllRepay($repayInfo){
        if($repayInfo === false){
            $returnData['flag'] = false;
            $returnData['msg'] = '不能进行正常还款';
            return $returnData;
        }
        if($repayInfo->period  === $repayInfo->period_count){
            $returnData['flag'] = false;
            $returnData['msg'] = '最后一期不能进行提前结清';
            return $returnData;
        }
        if($repayInfo->period  === 1){
            $returnData['flag'] = false;
            $returnData['msg'] = '第一期不能进行提前结清';
            return $returnData;
        }
        if($repayInfo->recordOne->status == RepayRecord::STATUS_TWO){
            //代表这条记录还在轮询中,不要还款
            $returnData['flag'] = false;
            $returnData['msg'] = '正在还款中,不能发起新的还款';
            return $returnData;
        }
        $day = count_days(Carbon::now()->toDateString(),$repayInfo->recordOne->normal_date);
        if($day > 0){
            $returnData['flag'] = false;
            $returnData['msg'] = '逾期不能进行提前结清';
            return $returnData;
        }
        //判断是否可以全部还清
        $isAllRepay = RepaySummary::isAllRepay($repayInfo->recordOne);
        if(!$isAllRepay){
            $returnData['flag'] = false;
            $returnData['msg'] = '当前状态不能进行提前结清';
            return $returnData;
        }

        $returnData['flag'] = true;
        $returnData['msg'] = '';
        return $returnData;
    }
}